package com.vooda.service.impl;

import com.vooda.constant.ErrorCode;
import com.vooda.dao.*;
import com.vooda.dto.bo.TicketBo;
import com.vooda.dto.vo.BaseResult;
import com.vooda.dto.vo.HappyBagVo;
import com.vooda.dto.vo.MyTicketVo;
import com.vooda.dto.vo.TicketVo;
import com.vooda.entity.*;
import com.vooda.service.TicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author KennyDavid
 */

@Service
public class TicketServiceImpl implements TicketService {

    private TicketDao ticketDao;
    private TicketTypeDao ticketTypeDao;
    private UserTicketDao userTicketDao;
    private StoreDao storeDao;
    private UserDao userDao;

    public TicketServiceImpl(TicketDao ticketDao, TicketTypeDao ticketTypeDao, UserTicketDao userTicketDao, StoreDao storeDao, UserDao userDao) {
        this.ticketDao = ticketDao;
        this.ticketTypeDao = ticketTypeDao;
        this.userTicketDao = userTicketDao;
        this.storeDao = storeDao;
        this.userDao = userDao;
    }

    @Override
    public BaseResult<List<TicketVo>> ticketList(Long storeId) {
        List<TicketVo> dtos = new ArrayList<>();
        TicketVo ticketVo;
        List<Ticket> tickets = ticketDao.findAllByIsDelAndStoreIdOrderByCreateTimeDesc(false, storeId);
        for (Ticket ticket : tickets) {
            if (ticket.getIsForever() || ticket.getEndTime().getTime() > System.currentTimeMillis()) {
                ticketVo = new TicketVo();
                BeanUtils.copyProperties(ticket, ticketVo);
                if (!ObjectUtils.isEmpty(ticketTypeDao.findOne(ticket.getTypeId()))) {
                    ticketVo.setType(ticketTypeDao.findOne(ticket.getTypeId()).getName());
                }
                dtos.add(ticketVo);
            }
        }
        return new BaseResult<>(dtos);
    }

    @Override
    public BaseResult<List<MyTicketVo>> myTicket(Long userId, Integer status) {
        List<MyTicketVo> dtos = new ArrayList<>();
        MyTicketVo myTicketVo;
        List<UserTicket> list = userTicketDao.findAllByUserIdAndStatusOrderByCreateTimeDesc(userId, status);
        for (UserTicket userTicket : list) {
            myTicketVo = new MyTicketVo();
            if (!ObjectUtils.isEmpty(userTicket.getTicketId())) {
                Ticket ticket = ticketDao.findOne(userTicket.getTicketId());
                if (!ObjectUtils.isEmpty(ticket)) {
                    Store store = storeDao.findOne(ticket.getStoreId());
                    if (!ObjectUtils.isEmpty(store)) {
                        BeanUtils.copyProperties(store, myTicketVo);
                        myTicketVo.setImageUrl("https://yihubaiying.org/data_cdn/" + store.getImageUrl());
                    }
                    BeanUtils.copyProperties(ticket, myTicketVo);
                    TicketType type = ticketTypeDao.findOne(ticket.getTypeId());
                    if (!ObjectUtils.isEmpty(type)) {
                        myTicketVo.setTicketType(type.getName());
                    }
                }
            }
            myTicketVo.setUserId(userId);
            dtos.add(myTicketVo);
        }
        return new BaseResult<>(dtos);
    }

    @Override
    public BaseResult getTicket(Long userId, Long ticketId) {
        UserTicket userTicket;
        userTicket = userTicketDao.findByUserIdAndTicketIdAndStatus(userId, ticketId, 1);
        if (ObjectUtils.isEmpty(userTicket)) {
            userTicket = new UserTicket();
            userTicket.setUserId(userId);
            userTicket.setTicketId(ticketId);
            userTicket.setStatus(1);
            userTicketDao.saveAndFlush(userTicket);
        } else {
            return new BaseResult(ErrorCode.HAS_GET_TICKET.getCode(), ErrorCode.HAS_GET_TICKET.getMsg());
        }
        return new BaseResult(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public BaseResult<List<HappyBagVo>> happyBag(Long userId) {
        //User one = userDao.findOne(userId);
        //if (!one.getIsGetBag()) {
        List<HappyBagVo> dtos = new ArrayList<>();
        HappyBagVo happyBagVo;
        UserTicket userTicket;
        List<Ticket> happyBag = ticketDao.findAllByIsHappyBagAndIsDel(true, false);
        for (Ticket ticket : happyBag) {
            userTicket = userTicketDao.findByUserIdAndTicketIdAndStatus(userId, ticket.getId(), 1);
            if (ObjectUtils.isEmpty(userTicket)) {
                happyBagVo = new HappyBagVo();
                userTicket = new UserTicket();
                userTicket.setTicketId(ticket.getId());
                userTicket.setUserId(userId);
                userTicket.setStatus(1);
                userTicketDao.saveAndFlush(userTicket);

                Store store = storeDao.findOne(ticket.getStoreId());
                if (!ObjectUtils.isEmpty(store)) {
                    happyBagVo.setName(store.getName());
                }

                TicketType type = ticketTypeDao.findOne(ticket.getTypeId());
                if (!ObjectUtils.isEmpty(type)) {
                    happyBagVo.setType(type.getName());
                }
                happyBagVo.setPrice(ticket.getPrice());
                dtos.add(happyBagVo);
            }
        }
        //  one.setIsGetBag(true);
        //userDao.saveAndFlush(one);
        return new BaseResult<>(dtos);
    //}
      //  return new BaseResult<>(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public BaseResult scan(Long userId, TicketBo ticketBo) {
        if (!userDao.findOne(userId).getIsCashier()) {
            return new BaseResult(ErrorCode.NOT_CASHIER.getCode(), ErrorCode.NOT_CASHIER.getMsg());
        }

        User user = userDao.findByOpenIdAndIsDel(ticketBo.getOpenId(), false);
        if (ObjectUtils.isEmpty(user)) {
            return new BaseResult(ErrorCode.NO_PERSON.getCode(), ErrorCode.NO_PERSON.getMsg());
        }
        UserTicket ticket = userTicketDao.findByUserIdAndTicketIdAndStatus(user.getId(), ticketBo.getTicketId(), 1);
        if (ticket.getStatus() == 3) {
            return new BaseResult(ErrorCode.TICKET_OUTDATE.getCode(), ErrorCode.TICKET_OUTDATE.getMsg());
        }
        if (ObjectUtils.isEmpty(ticket)) {
            return new BaseResult(ErrorCode.NOT_FOUND_USER_TICKET.getCode(), ErrorCode.NOT_FOUND_USER_TICKET.getMsg());
        }
        ticket.setStatus(2);
        userTicketDao.saveAndFlush(ticket);
        return new BaseResult(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
    }

    @Override
    public BaseResult<MyTicketVo> myTicketDetail(Long userId, Long ticketId) {
        Ticket ticket = ticketDao.findOne(ticketId);
        MyTicketVo myTicketVo = new MyTicketVo();
        BeanUtils.copyProperties(ticket, myTicketVo);
        myTicketVo.setUserId(userId);
        myTicketVo.setTicketType(ticketTypeDao.findOne(ticket.getTypeId()).getName());
        Store store = storeDao.findOne(ticket.getStoreId());
        if (!ObjectUtils.isEmpty(store)) {
            myTicketVo.setName(store.getName());
            myTicketVo.setImageUrl(store.getImageUrl());
        }
        return new BaseResult<>(myTicketVo);
    }
}
